<?php

class PostController extends Zend_Controller_Action
{
    public function indexAction ()
    {   
        
    }
    
    public function viewAction()
    {
        $postId = $this->_getParam('id', null);
        $postService = Service_Post::getInstance();
        $emptyComment = new Model_Comment(array('post_id'=>$postId));
        $commentForm = new Form_Comment($emptyComment);
        $this->view->assign(array('post' => $postService->findPost($postId),'form'=>$commentForm));
    }
    
    public function updateAction()
    {
        $postService = Service_Post::getInstance();
        $postId = $this->_getParam('id', null);
        $form = new Form_Post($postService->findPost($postId));
        if($this->getRequest()->isPost()){
            if($form->isValid($this->getRequest()->getPost())){
                $post = new Model_Post($this->getRequest()->getPost());
                $post->id = $postId;
                $post = $postService->savePost($post);
                $this->_redirect('/post/view/id/' . $post->id);
            }
        }
        $this->view->form = $form;
    }
    
    public function deleteAction()
    {
        $postService = Service_Post::getInstance();
        $post = $postService->findPost($this->_getParam('id', null));
        $postService->deletePost($post);
        $this->_redirect('/');
    }
    
    public function syncAction()
    {
        $postService = Service_Post::getInstance();
        $postService->syncPosts();
    }
}